﻿#include "widget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QDebug>
#include <QMouseEvent>
#include "skin/skin.h"

WeChat::WeChat(QWidget *parent)
    : QWidget{parent}
{
    this->setFixedSize(350,475);
    this->setWindowFlags(Qt::FramelessWindowHint);

    setMouseTracking(true);

    QWidget * titleWdt=new QWidget(this);

    titleWdt->setAttribute(Qt::WA_StyledBackground);
    QWidget * centerWgt = new QWidget(this);
    QWidget * LoginWgt = new QWidget(this);
    QWidget * tailWgt =new QWidget(this);

    //设置子窗口大小
    titleWdt->setFixedSize(350,30);
    centerWgt->setFixedSize(350,325);
    LoginWgt->setFixedSize(350,80);
    tailWgt->setFixedSize(350,40);

    /*titleWgt*/
    auto nameLabel =new QLabel(titleWdt);
    settingBtn = new QPushButton(titleWdt);
    closeBtn = new QPushButton(titleWdt);

    //设置按钮名字
    settingBtn->setObjectName("settingBtn");
    closeBtn->setObjectName("closeBtn");


    //实现按钮功能
    //setting设为最小化
    connect(settingBtn,&QPushButton::clicked,this,[this](){
        this->showMinimized();
    });
    //closeBtn设为关闭程序
    connect(closeBtn,&QPushButton::clicked,this,[this](){
        this->close();
    });

    nameLabel->setFixedSize(30,30);
    nameLabel->setText("微信");
    settingBtn->setFixedSize(30,30);
    closeBtn->setFixedSize(30,30);
    //设置titleWgt的水平布局
    auto titleWgtHLayout = new QHBoxLayout(titleWdt);
    titleWgtHLayout->addWidget(nameLabel);
    titleWgtHLayout->addStretch(1); //加入弹簧将标签与按钮分开
    titleWgtHLayout->addWidget(settingBtn);
    titleWgtHLayout->addWidget(closeBtn);
    titleWgtHLayout->setContentsMargins(10,0,0,0);
    titleWdt->setLayout(titleWgtHLayout);

    /* center Wgt*/
    auto imageLabel =new QLabel(centerWgt);
    auto userLabel =new QLabel(centerWgt);
    imageLabel->setFixedSize(100,100);
    imageLabel->setObjectName("imageLabel");
    userLabel->setFixedSize(100,40);
    userLabel->setText("张三");
    userLabel->setAlignment(Qt::AlignHCenter);
    //添加centerWgt的布局
    auto centerWgtLayout = new QVBoxLayout(centerWgt);
    centerWgtLayout->addWidget(imageLabel);
    centerWgtLayout->addWidget(userLabel);
    centerWgtLayout->setAlignment(Qt::AlignHCenter);
    centerWgt->setLayout(centerWgtLayout);

    /* LoginWgt*/
    auto LoginBtn = new QPushButton(LoginWgt);
    LoginBtn->setFixedSize(300,40);
    LoginBtn->setText("登录");
    LoginBtn->setObjectName("LoginBtn");

    //设置信号与槽
    connect(LoginBtn,&QPushButton::clicked,this,[this](){
        if(player_)
        {
            return;
        }
        player_ = new PlayerWgt();
        player_->show();
        this->hide();
    });
    //添加LoginWgt的水平布局
    auto loginLayout = new QHBoxLayout(LoginWgt);
    loginLayout->addWidget(LoginBtn);
    loginLayout->setContentsMargins(0,0,0,0);
    loginLayout->setAlignment(Qt::AlignHCenter);
    LoginWgt->setLayout(loginLayout);

    /*tailWgt*/
    QPushButton* acountBtn = new QPushButton(tailWgt);
    QPushButton* fileBtn = new QPushButton(tailWgt);
    //设置按钮属性
    acountBtn->setFixedSize(100,40);
    fileBtn->setFixedSize(100,40);
    //添加taileWgt的水平布局
    auto taileLayout = new QHBoxLayout(tailWgt);
    taileLayout->addWidget(acountBtn);
    taileLayout->addWidget(fileBtn);
    taileLayout->setContentsMargins(0,0,0,0);
    tailWgt->setLayout(taileLayout);

    //总体使用垂直布局
    auto vLayout = new QVBoxLayout(this);
    vLayout->addWidget(titleWdt);
    vLayout->addWidget(centerWgt);
    vLayout->addWidget(LoginWgt);
    vLayout->addWidget(tailWgt);
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0,0,0,0);
    this->setLayout(vLayout);

    SkinMgr::inst()->setSkin(SK_Main,this);
}

void WeChat::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton && isPress_)
    {
        move(event->globalPos() - point_);
    }
    QWidget::mouseMoveEvent(event);
}

void WeChat::mousePressEvent(QMouseEvent *event)
{
    isPress_ = true;
    point_ = event->globalPos() - this->frameGeometry().topLeft();
    QWidget::mousePressEvent(event);
}

void WeChat::mouseReleaseEvent(QMouseEvent *event)
{
    isPress_ = false;
    QWidget::mouseReleaseEvent(event);
}
